package com.simple.microservices.web.auth;


import com.simple.microservices.commons.exceptions.BusinessException;
import com.simple.microservices.commons.utils.Response;
import com.simple.microservices.manager.AuthManager;
import com.simple.microservices.web.auth.dto.LoginDTO;
import com.simple.microservices.web.convert.AuthConvert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 授权管理
 * @author HYF
 */
@RequestMapping("auth")
@RestController
public class AuthController {
    @Resource
    private AuthManager authManger;
    /**
     * 用户登录
     *
     * @param loginDTO
     * @return
     */
    @PostMapping("login/unauth")
    public Response login(@Validated @RequestBody LoginDTO loginDTO, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(loginDTO.getPassword())&&StringUtils.isEmpty(loginDTO.getVerificationCode())){
            throw new BusinessException("验证码或密码为空");
        }
        authManger.login(AuthConvert.INSTANCE.convert(loginDTO),request,response);
        return Response.success();
    }


}
